package com.newenergy.wisehome.gateway.account;


import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.common.helper.StorageHelper;
import com.newenergy.wisehome.config.BaseApp;
import com.newenergy.wisehome.data.bean.UserInfoItem;
import com.newenergy.wisehome.gateway.adapter.UserInfoAdapter;
import com.newenergy.wisehome.gateway.device.FamilyBLDeviceFragment;
import com.newenergy.wisehome.gateway.device.PairDeviceFragment;
import com.newenergy.wisehome.gateway.device.SearchDnaDeviceFragment;
import com.newenergy.wisehome.gateway.family.BLFamilyListFragment;
import com.newenergy.wisehome.gateway.manager.BLLocalFamilyManager;
import com.newenergy.wisehome.ui.fragment.BaseFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class UserInfoFragment extends BaseFragment {

    @BindView(R.id.rv_user_info)
    EasyRecyclerView mRecyclerView;
    private ArrayList<UserInfoItem> mData;
    private UserInfoAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_user_info;
    }

    @Override
    public void init(View rootView) {
        mData = new ArrayList<>();
        mAdapter = new UserInfoAdapter(getContext());
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        mAdapter.setOnItemClickListener(position -> {
            UserInfoItem infoItem = mAdapter.getItem(position);
            BLLocalFamilyManager familyManager = BLLocalFamilyManager.getInstance();
            String blFamilyId = familyManager.mBLFamilyId;
            switch (infoItem.getTitle()) {
                case "账号：":
                    break;
                case "昵称：":
                    break;
                case "设备配网":
                    start(new PairDeviceFragment());
                    break;
                case "搜索设备":
                    start(new SearchDnaDeviceFragment());
                    break;
                case "当前家庭的BL设备":
                    if (TextUtils.isEmpty(blFamilyId)) {
                        toast("blFamilyId 为空");
                        return;
                    }
                    familyManager.queryFamilyAllInfo(blFamilyId);
                    start(new FamilyBLDeviceFragment());
                    break;
                case "BL家庭列表":
//                        familyManager.queryFamilyAllInfo(blFamilyId);
                    familyManager.queryFamilyAllInfo(blFamilyId);
                    start(new BLFamilyListFragment());
                    break;
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mAdapter.clear();
        String phone = BaseApp.getmBLUserInfoUnits().getPhone();
        String nickname = BaseApp.getmBLUserInfoUnits().getNickname();
        mAdapter.add(new UserInfoItem("账号：", phone));
        mAdapter.add(new UserInfoItem("昵称：", nickname));
    }

    @OnClick(R.id.btn_login_out)
    public void clickLoginOut() {
        loginOut();
        doBack();
    }

    private void loginOut() {
        BaseApp.getmBLUserInfoUnits().loginOut();
        StorageHelper.setBLAccountInfo(null);
        BLLocalFamilyManager.getInstance().isLogin = false;

    }

    @OnClick({R.id.btn_back, R.id.tv_back, R.id.tv_switch_account})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                doBack();
                break;
            case R.id.tv_back:
                doBack();
                break;
            case R.id.tv_switch_account:
                switchAccount();
                break;
        }
    }

    private void switchAccount() {
        loginOut();
        start(new LoginFragment());
    }
}
